# coding: utf-8


class SymbolTable(object):
    pass


class GlobalSymbolTable(SymbolTable):
    __table = None
    __node_mapping = None

    def __init__(self):
        self.__table = {}
        self.__node_mapping = {}

    def __repr__(self):
        return str(self.__table)

    def __getitem__(self, item):
        return self.__table[item]

    def is_exist_key(self, key):
        if key in self.__table:
            return True
        return False
    # INTERFACE

    def add_symbol(self, syntax_tree_node, name, symbol_type):
        self.__table[name] = symbol_type
        self.__node_mapping[name] = syntax_tree_node


class LocalSymbolTable(SymbolTable):
    __table = None
    __global_table = None
    __global_table_mapping = None

    def __init__(self, global_table):
        self.__table = {}
        self.__global_table = global_table
        self.__global_table_mapping = {}

    def __repr__(self):
        return str(self.__table)

    # INTERFACE

    def add_symbol(self, syntax_tree_node, symbol_name, symbol_type):
        if symbol_name in self.__table:
            return
        self.__table[symbol_name] = symbol_type
        self.__global_table_mapping[symbol_name] = self.__global_table.add_symbol(syntax_tree_node, symbol_name,
                                                                                  symbol_type)

    def has_symbol(self, symbol_name):
        res = [symbol_name in self.__table, None]
        if res[0]:
            res[1] = self.__table[symbol_name]
        return res